import { onBeforeUnmount, ref } from 'vue'
import { supermapReady } from '/@packages/components'
import { selectCursor, defaultCursor } from '/@src/utils'
export default function () {
  const { $myapp } = supermapReady()
  // 是否开启选择顶点
  const pickPointEnabled = ref(true)
  // 是否开启等高线
  const setHypFlag = ref(true)
  onBeforeUnmount(() => {
    onClearAll()
  })
  //#region 共用方法
  let currentHandler = null
  // 改变鼠标样时，并设置量算是否可捕捉
  const activeMouse = (isActive: boolean) => {
    if (isActive) {
      selectCursor($myapp.viewer)
      currentHandler!.snappingEnabled = pickPointEnabled.value
    } else {
      defaultCursor($myapp.viewer)
      currentHandler!.snappingEnabled = false
    }
  }
  // 清除所有结果
  const clearAll = () => {
    deactiveAll()
    handlerDis && handlerDis.clear()
    handlerArea && handlerArea.clear()
    handlerHeight && handlerHeight.clear()
  }
  // 古纳比量测处理器
  const deactiveAll = () => {
    handlerDis && handlerDis.deactivate()
    handlerArea && handlerArea.deactivate()
    handlerHeight && handlerHeight.deactivate()
  }
  // 点击量算距离事件
  const onMeasureDis = () => {
    deactiveAll()
    if (handlerDis) {
      currentHandler = handlerDis
      handlerDis.activate()
    }
  }
  // 点击量算距离事件
  const onMeasureArea = () => {
    deactiveAll()
    if (handlerArea) {
      currentHandler = handlerArea
      handlerArea.activate()
    }
  }
  // 点击量算距离事件
  const onMeasureHeight = () => {
    deactiveAll()
    if (handlerHeight) {
      currentHandler = handlerHeight
      handlerHeight.activate()
    }
  }
  // 点击清除事件
  const onClearAll = () => {
    clearAll()
  }
  //#endregion

  //#region 测距开始
  //初始化测量距离
  const handlerDis = new SuperMap3D.MeasureHandler(
    $myapp.viewer,
    SuperMap3D.MeasureMode.Distance,
    0
  )
  //注册测距功能事件
  handlerDis.measureEvt.addEventListener(function (result) {
    const dis = Number(result.distance)
    const distance =
      dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm'
    handlerDis.disLabel.text = '距离:' + distance
  })
  handlerDis.activeEvt.addEventListener(function (isActive) {
    activeMouse(isActive)
  })
  //#endregion结束

  //#region 测面开始
  //初始化测量面积
  const handlerArea = new SuperMap3D.MeasureHandler(
    $myapp.viewer,
    SuperMap3D.MeasureMode.Area,
    0
  )
  handlerArea.measureEvt.addEventListener(function (result) {
    var mj = Number(result.area)
    var area =
      mj > 1000000 ? (mj / 1000000).toFixed(2) + 'km²' : mj.toFixed(2) + '㎡'
    handlerArea.areaLabel.text = '面积:' + area
  })
  handlerArea.activeEvt.addEventListener(function (isActive) {
    activeMouse(isActive)
  })
  //#endregion结束

  //#region 测高开始
  //初始化测量高度
  const handlerHeight = new SuperMap3D.MeasureHandler(
    $myapp.viewer,
    SuperMap3D.MeasureMode.DVH
  )

  handlerHeight.measureEvt.addEventListener(function (result) {
    var distance =
      result.distance > 1000
        ? (result.distance / 1000).toFixed(2) + 'km'
        : result.distance + 'm'
    var vHeight =
      result.verticalHeight > 1000
        ? (result.verticalHeight / 1000).toFixed(2) + 'km'
        : result.verticalHeight + 'm'
    var hDistance =
      result.horizontalDistance > 1000
        ? (result.horizontalDistance / 1000).toFixed(2) + 'km'
        : result.horizontalDistance + 'm'
    handlerHeight.disLabel.text = '空间距离:' + distance
    handlerHeight.vLabel.text = '垂直高度:' + vHeight
    handlerHeight.hLabel.text = '水平距离:' + hDistance
  })

  handlerHeight.activeEvt.addEventListener(function (isActive) {
    activeMouse(isActive)
  })
  //#endregion结束
  return {
    pickPointEnabled,
    setHypFlag,
    onMeasureDis,
    onMeasureArea,
    onMeasureHeight,
    onClearAll
  }
}
